package com.synv.util;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.os.Environment;

import com.synv.entity.FlashCard;

public class WebParserUtils {

	public static final String DEFAULT_FOLDER = Environment.getExternalStorageDirectory() + "/flashcard/";
	public static Document getDocument(String word) {
		try {
			String url = "http://tratu.soha.vn/dict/en_vn/" + word;
			return Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}
	
	public static  String getPhonetic(Document doc) {
		Elements elements = doc.select("html body.mediawiki div#globalWrapper div.header-page div.main-content ul.testMonobook2 li div#content-main div#column-content div#content div#bodyContent div#content-5.section-h5 h5 span.mw-headline b font");
		return elements.get(0).text();
	}
	
	public static Integer getWordType(Document doc) {
		Elements elements = doc.select("html body.mediawiki div#globalWrapper div.header-page div.main-content ul.testMonobook2 li div#content-main div#column-content div#content div#bodyContent div#show-alter.section-h2 div#content-3.section-h3 h3 span.mw-headline");
		Integer type = 0;
		for(Element element : elements) {
    	    String text = element.text();
    	    if(text.contains("danh từ")) type += FlashCard.NOUN;
    	    if(text.contains("động từ")) type += FlashCard.VERB;
    	    if(text.contains("tính từ")) type += FlashCard.ADJ;
    	    if(text.contains("trạng từ")) type += FlashCard.ADV;
    	    if(text.contains("Giới từ")) type += FlashCard.PREP;
    	}
		return type;
	}
	
	public static String getMeaning(Document doc) {
		Elements elements = doc.select("html body.mediawiki div#globalWrapper div.header-page div.main-content ul.testMonobook2 li div#content-main div#column-content div#content div#bodyContent div#show-alter.section-h2 div#content-3.section-h3 div#content-5.section-h5 h5 span.mw-headline");
		String text =  elements.get(0).text();
		return text;
	}
	
	public static String getMp3(Document doc) {
		String mp3 = "";
		Elements elements = doc.select("html body.mediawiki div#globalWrapper div.header-page div.main-content ul.testMonobook2 li div#content-main div#column-content div#content h1.firstHeading object#player embed");
		if(elements.size() == 0) return "";
    	Element element = elements.first();
    	mp3 = element.attr("flashvars");
    	if(mp3 == null) return "";
    	mp3 = mp3.substring(mp3.indexOf("http"), mp3.indexOf(".mp3"));
    	mp3 += ".mp3";
		return mp3; 
    }
	
	public static void storeMp3(String word) {
		try {
			Document doc = WebParserUtils.getDocument(word);
			String mp3 = getMp3(doc);
			URL url = new URL(mp3);
			InputStream is = url.openStream();
            DataInputStream dis = new DataInputStream(is);
            byte[] buffer = new byte[1024];
            int length;
            File file = new File(Environment.getExternalStorageDirectory() + "/flashcard/" + word + ".mp3");
            FileOutputStream fos = new FileOutputStream(file);
            while ((length = dis.read(buffer))>0) {
              fos.write(buffer, 0, length);
            }
            dis.close();
            is.close();
            fos.close();
		} catch (Exception ex) {
			
		}
	}
}
